package splug.cjk;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.*;
import java.util.ArrayList;

import splug.cjk.packet.*;

public class Server {

	private static ServerSocket server;
	private static BufferedInputStream br;
	private static Socket client;

	public static void main(String args[]){

		try {
			server = new ServerSocket(2007);		

			while(true){				
				System.out.println("클라이언트 대기중..");

				client = server.accept();

				System.out.println("클라이언트 접속함 : " + client.getInetAddress());

				InputStream is = client.getInputStream();
				OutputStream os = client.getOutputStream();

				InputStreamReader isr = new InputStreamReader(is);
				OutputStreamWriter osw = new OutputStreamWriter(os);

				int req = isr.read();
				
				/**
				 * req == 0 : 회원가입 요청
				 * req == 1 : 로그인 요청
				 * req == 2 : 자유게시판 게시물 읽기 요청
				 * req == 3 : 자유게시판 게시물 쓰기 요청
				 * req == 4 : 공지사항 게시물 읽기 요청
				 * req == 5 : 카풀 게시판 탭1 읽기 요청
				 * req == 6 : 카풀 게시판 탭1 쓰기 요청
				 * req == 7 : 카풀 게시판 탭2 읽기 요청
				 * req == 8 : 카풀 게시판 탭2 쓰기 요청
				 * req == 9 : 카풀 게시판 탭1 댓글 읽기 요청
				 * req == 10 : 카풀 게시판 탭1 댓글 쓰기 요청
				 * req == 11 : 카풀게시판 탭2 댓글 읽기 요청
				 * req == 12 : 카풀게시판 탭2 댓글 쓰기 요청
				 * req == 13 : 친구 목록 읽기 요청
				 * req == 14 : 친구 요청 목록 읽기 요청
				 * req == 15 : 친구 요청 수락 요청
				 * req == 16 : 친구 추가 요청
				 * req == 17 : 쪽지 목록 읽기 요청
				 * req == 18 : 쪽지 쓰기
				 * req == 19 : 내가 쓴 자유게시판 글 목록 요청
				 * req == 20 : 내가 쓴 카풀게시판 탭1 읽어오기
				 * req == 21 : 내가 쓴 카풀게시판 탭2 읽어오기
				 * req == 22 : 신고 하기
				 * req == 23 : 회원정보 보기 요청
				 * req == 24 : 신고 현황 불러오기
				 */

				if(req == '0'){	
					System.out.println("ASSIGN connect!");
					
					osw.write('1'); 
					osw.flush();
					
					ObjectInputStream ois = new ObjectInputStream(is);
					AssignPacket packet = (AssignPacket) ois.readObject();
					
					String id = packet.getID();
					String pw = packet.getPW();
					String name = packet.getName();
					String car = packet.getCar();
					String phone = packet.getPhone();
					String mail = packet.getMail();
					
					Assign assign = new Assign(id,pw,name,car,phone,mail,osw);
					assign.start();

				}

				else if(req == '1'){

					System.out.println("LOGIN connect!");

					osw.write('1');
					osw.flush();

					ObjectInputStream input = new ObjectInputStream(is);
					LoginPacket packet = (LoginPacket) input.readObject();

					String id = packet.getID();
					String pw = packet.getPW();

					Login login = new Login(id, pw, osw);
					login.start();
				}
				else if(req == '2'){
					System.out.println("BBS Free Read connect!");
					
					osw.write('1');
					osw.flush();
					
					BBSFreeRead free = new BBSFreeRead(os);
					free.start();
				}
				else if(req == '3'){
					System.out.println("BBS Free Write connect!");
					
					osw.write('1');
					osw.flush();
					
					ObjectInputStream input = new ObjectInputStream(is);
					BBSPacket packet = (BBSPacket) input.readObject();
					
					int number = packet.getNumber();
					String id = packet.getID();
					String title = packet.getTitle();
					String content = packet.getSubTitle();
					String date = packet.getDate();
					
					BBSFreeWrite thread = new BBSFreeWrite(is,os,number,id,title,content,date);
					thread.start();
				}
				else if(req == '4'){
					System.out.println("BBS Notice Read connect!");
					
					osw.write('1');
					osw.flush();
					
					BBSNoticeRead thread = new BBSNoticeRead(os);
					thread.start();
				}
				else if(req == '5'){
					System.out.println("CarPool Tab1 Read connect!");
					
					osw.write('1');
					osw.flush();
					
					CarPoolTab1Read thread = new CarPoolTab1Read(os);
					thread.start();
				}
				else if(req == '6'){
					System.out.println("CarPool Tab1 Write connect!");
					
					osw.write('1');
					osw.flush();
					
					ObjectInputStream input = new ObjectInputStream(is);
					BBSCarPoolPacket packet = (BBSCarPoolPacket) input.readObject();
					
					String id = packet.getId();
					String title = packet.getTitle();
					String start = packet.getStart();
					String finish = packet.getFinish();
					String time = packet.getTime();
					String content = packet.getSub_title();
					String date = packet.getDate();
					double latitude = packet.getLatitude();
					double longitude = packet.getLongitude();
					
					CarPoolTab1Write thread = new CarPoolTab1Write(is, os, id, title, start, finish, time, content, date, latitude, longitude);
					thread.start();
				}
				else if(req == '7'){
					System.out.println("CarPool Tab2 Read connect!");
					
					osw.write('1');
					osw.flush();
					
					CarPoolTab2Read thread = new CarPoolTab2Read(os);
					thread.start();
				}
				else if(req == '8'){
					System.out.println("CarPool Tab2 Write connect!");
					
					osw.write('1');
					osw.flush();
					
					ObjectInputStream input = new ObjectInputStream(is);
					BBSCarPoolPacket packet = (BBSCarPoolPacket) input.readObject();
					
					String id = packet.getId();
					String title = packet.getTitle();
					String start = packet.getStart();
					String finish = packet.getFinish();
					String time = packet.getTime();
					String content = packet.getSub_title();
					String date = packet.getDate();
					double latitude = packet.getLatitude();
					double longitude = packet.getLongitude();
					
					CarPoolTab2Write thread = new CarPoolTab2Write(is, os, id, title, start, finish, time, content, date, latitude, longitude);
					thread.start();
				}
				else if(req == '9'){
					System.out.println("CarPool Tab1 Comment Read connect!");
					
					osw.write('1');
					osw.flush();
					
					int number = isr.read();
					
					CarPoolTab1CommentRead thread = new CarPoolTab1CommentRead(os, number);
					thread.start();
				}
				else if(req == 10){
					System.out.println("CarPool Tab1 Comment Write connect!");
					
					osw.write('1');
					osw.flush();
					
					ObjectInputStream input = new ObjectInputStream(is);
					BBSCommentPacket packet = (BBSCommentPacket) input.readObject();
					
					String id = packet.getId();
					String comment = packet.getComment();
					int number = packet.getNumber();
					
					CarPoolTab1CommentWrite thread = new CarPoolTab1CommentWrite(is, os, number, id, comment);
					thread.start();
				}
				else if(req == 11){
					System.out.println("CarPool Tab2 Comment Read connect!");
					
					osw.write('1');
					osw.flush();
					
					int number = isr.read();
					
					CarPoolTab2CommentRead thread = new CarPoolTab2CommentRead(os, number);
					thread.start();
				}
				else if(req == 12){
					System.out.println("CarPool Tab2 Comment Write connect!");
					
					osw.write('1');
					osw.flush();
					
					ObjectInputStream input = new ObjectInputStream(is);
					BBSCommentPacket packet = (BBSCommentPacket) input.readObject();
					
					String id = packet.getId();
					String comment = packet.getComment();
					int number = packet.getNumber();
					
					CarPoolTab2CommentWrite thread = new CarPoolTab2CommentWrite(is, os, number, id, comment);
					thread.start();
				}
				else if(req == 13){
					System.out.println("Friend list Read connect!");
					
					osw.write('1');
					osw.flush();
					
					ObjectInputStream input = new ObjectInputStream(is);
					FriendPacket packet = (FriendPacket) input.readObject();
					
					String id = packet.getId();
					
					System.out.println(id);
					
					FriendListRead thread = new FriendListRead(os, id);
					thread.start();
				}
				else if(req == 14){
					System.out.println("친구 요청 목록 읽기 요청 접속됨!");
					
					osw.write('1');
					osw.flush();
					
					ObjectInputStream input = new ObjectInputStream(is);
					FriendPacket packet = (FriendPacket) input.readObject();
					
					String id = packet.getId();
					
					System.out.println(id);
					
					FriendRequestRead thread = new FriendRequestRead(os, id);
					thread.start();
				}
				else if(req == 15){
					System.out.println("친구 요청 수락 요청 접속됨!");
					
					osw.write('1');
					osw.flush();
					
					ObjectInputStream input = new ObjectInputStream(is);
					FriendPacket packet = (FriendPacket) input.readObject();
					
					String id = packet.getId();
					String you = packet.getYou();
					
					System.out.println(id);
					
					FriendRequestAccept thread = new FriendRequestAccept(os, id, you);
					thread.start();
				}
				else if(req == 16){
					System.out.println("친구추가 요청 connect!");
					
					osw.write('1');
					osw.flush();
					
					FriendAdd thread = new FriendAdd(is, os);
					thread.start();
				}
				else if(req == 17){
					System.out.println("쪽지 목록 요청 connect!");
					
					osw.write('1');
					osw.flush();
					
					MessageRead thread = new MessageRead(is, os);
					thread.start();
				}
				else if(req == 18){
					System.out.println("쪽지 쓰기 connect!");
					
					osw.write('1');
					osw.flush();
					
					MessageWrite thread = new MessageWrite(is, os);
					thread.start();
				}
				else if(req == 19){
					System.out.println("내가 작성한 자유게시판 목록 connect!");
					
					osw.write('1');
					osw.flush();
					
					BBSMyFreeRead free = new BBSMyFreeRead(is, os);
					free.start();
				}
				else if(req == 20){
					System.out.println("내가 작성한 태워드립니다 게시판 목록 connect!");
					
					osw.write('1');
					osw.flush();
					
					MyCarPoolTab1Read thread = new MyCarPoolTab1Read(is, os);
					thread.start();
				}
				else if(req == 21){
					System.out.println("내가 작성한 태워드립니다 게시판 목록 connect!");
					
					osw.write('1');
					osw.flush();
					
					MyCarPoolTab2Read thread = new MyCarPoolTab2Read(is, os);
					thread.start();
				}
				else if(req == 22){
					System.out.println("신고하기 connect!");
					
					osw.write('1');
					osw.flush();
					
					ReportWrite thread = new ReportWrite(is, os);
					thread.start();
				}
				else if(req == 23){
					System.out.println("정보보기 connect!");
					
					osw.write('1');
					osw.flush();
					
					PersonInfoRead thread = new PersonInfoRead(is, os);
					thread.start();
				}
				else if(req == 24){
					System.out.println("신고 현황 불러오기 connect!");
					
					osw.write('1');
					osw.flush();
					
					ReportRead thread = new ReportRead(is, os);
					thread.start();
				}
				else{
					osw.write('0');
					osw.flush();
				}
			}


		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}
}
